package com.delayqueue.master.netty.handler;

import com.delayqueue.common.enums.MessageType;
import com.delayqueue.common.netty.Address;
import com.delayqueue.common.netty.NettyMessage;
import com.delayqueue.common.netty.NettyUtil;
import com.delayqueue.master.cache.NodeCacheFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MessageInboundHandler extends SimpleChannelInboundHandler<NettyMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, NettyMessage message) throws Exception {
        MessageType messageType = message.getMessageType();
        if (messageType == MessageType.CORE_ONLINE) {
            Address address = NettyUtil.getRemoteAddress(channelHandlerContext);
            NodeCacheFactory.core.add(address.getIp(), address.getPort(), channelHandlerContext);
        }
    }
}
